package com.jkt.rms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jkt.rms.businessservice.UserBusinessService;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.commons.utils.ServiceLocator;
import com.jkt.rms.vo.UsersVO;

public class ChangePasswordController extends SimpleFormController {

	private static final Logger LOG = Logger.getLogger(ChangePasswordController.class);
	
	private UserBusinessService userBusinessService;
	
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws RMSException {
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws RMSException {
		LOG.debug(this.getClass().getSimpleName());
		
		UsersVO userData = (UsersVO) command;
		userBusinessService = ServiceLocator.getServiceLocator().locate("userBusinessService");
		boolean status = userBusinessService.updateUser(userData);
		if(status) {
			 userData.setMessage("Password has been changed successfully.");
		}
		return new ModelAndView("changePassword","user",userData);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws RMSException {
		UsersVO user = new UsersVO();
		return user;
	}
}
